package org.my.workflow.element;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.my.workflow.persistence.StepEntity;
import org.w3c.dom.Element;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
public class TargetStepElement implements ConfigElement {

    private String stepId;
    private String status = StepEntity.STATUS_UNDERAWAY;
    private int dueDate = -1;

    private List<ConditionElement> conditionElements = new ArrayList<ConditionElement>();

    public String getStepId() {
        return stepId;
    }

    public String getStatus() {
        return status;
    }

    public List<ConditionElement> getConditionElements() {
        return Collections.unmodifiableList(conditionElements);
    }

    public boolean isConditional() {
        return !conditionElements.isEmpty();
    }

    public int getDueDate() {
        return dueDate;
    }

    public void processXML(Element element) {
        stepId = XMLUtils.getAtrributeValue(element, "id");
        String _dueDate = XMLUtils.getAtrributeValue(element, "due-date");
        if(_dueDate != null && _dueDate.trim().length() != 0){
            dueDate = Integer.parseInt(_dueDate.trim());
        }
        String status = XMLUtils.getAtrributeValue(element, "status");

        if (status != null && status.trim().length() > 0) {
            this.status = status;
        }
        List<Element> conditionXMLElements = XMLUtils.getChildElementsByTagName(element, "condition");
        for (Element conditionXMLElement : conditionXMLElements) {
            if (conditionXMLElement != null) {
                ConditionElement conditionElement = new ConditionElement();
                conditionElement.processXML(conditionXMLElement);
                conditionElements.add(conditionElement);
            }
        }
    }

    public static void main(String[] args) {

    }
}
